/**
 * 导入审核 API 模块
 * 对应后端 /api/v1/imports 端点
 */
import { request } from '../request'
import type {
  ImportBatch,
  ImportRecord,
  ImportRecordUpdate,
  ImportBatchStatistics,
  BulkActionRequest,
  FinalizeResult,
} from '../types'

/**
 * 导入审核 API
 */
export const importAuditApi = {
  /**
   * 获取批次信息
   * GET /api/v1/imports/{batch_id}
   */
  getBatch(batchId: string): Promise<ImportBatch> {
    return request.get(`v1/imports/${batchId}`)
  },

  /**
   * 获取批次的导入记录列表
   * GET /api/v1/imports/{batch_id}/records
   */
  getRecords(
    batchId: string,
    params?: {
      skip?: number
      limit?: number
      status_filter?: string
    }
  ): Promise<ImportRecord[]> {
    return request.get(`v1/imports/${batchId}/records`, { params })
  },

  /**
   * 获取批次统计信息
   * GET /api/v1/imports/{batch_id}/statistics
   */
  getStatistics(batchId: string): Promise<ImportBatchStatistics> {
    return request.get(`v1/imports/${batchId}/statistics`)
  },

  /**
   * 更新单条导入记录
   * PATCH /api/v1/imports/records/{record_id}
   */
  updateRecord(recordId: number, data: ImportRecordUpdate): Promise<ImportRecord> {
    return request.patch(`v1/imports/records/${recordId}`, data)
  },

  /**
   * 批量操作导入记录
   * POST /api/v1/imports/{batch_id}/records/bulk-action
   */
  bulkAction(batchId: string, data: BulkActionRequest): Promise<{ affected_count: number }> {
    return request.post(`v1/imports/${batchId}/records/bulk-action`, data)
  },

  /**
   * Finalize 批次 (写入 leads 表)
   * POST /api/v1/imports/{batch_id}/finalize
   */
  finalizeBatch(batchId: string): Promise<FinalizeResult> {
    return request.post(`v1/imports/${batchId}/finalize`)
  },

  /**
   * 获取所有导入批次列表
   * GET /api/v1/imports/
   */
  listBatches(params?: { status_filter?: string }): Promise<ImportBatch[]> {
    return request.get('v1/imports/', { params })
  },
}
